package com.grouplay.service.impl;

import com.grouplay.dao.IInformationDao;
import com.grouplay.dao.IUserDao;
import com.grouplay.entity.Information;
import com.grouplay.entity.User;
import com.grouplay.service.IInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("informationService")
public class InformationServiceImpl implements IInformationService {

    @Autowired
    IUserDao userDao;
    @Autowired
    IInformationDao informationDao;

    @Override
    public boolean sendInformation(User sourceUser, String targetName, String message) {
        User targetUser = userDao.findUserByName(targetName);
        if (targetUser == null) {
            return false;
        }
        Information information = new Information();
        information.setSourceUid(sourceUser.getId());
        information.setTargetUid(targetUser.getId());
        information.setMessage(message);
        information.setHasBeenLooked(0);
        information.setDate(new Date());
        informationDao.insert(information);
        return true;
    }

    @Override
    public List<Information> findTenInformationByUser(User user) {
        return informationDao.findTenInformationByUser(user);
    }

    @Override
    public boolean setHasBeenLookedByIdAndTargetId(Integer id, Integer targetUid) {
        Information information = new Information();
        information.setId(id);
        information.setTargetUid(targetUid);
        informationDao.setHasBeenLookedByIdAndTargetId(information);
        return true;
    }
}
